<?php
	
function list_materi_pelatihan(){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=materidelete&id=\'+id;
			}
			return false;
		}
	');
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Materi pelatihan'=>array('style'=>'border-bottom:2px solid;width:60%'),    
		'Kategori'=>array('style'=>'border-bottom:2px solid;width:25%'),    
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);

	$query = "SELECT * FROM materi_pelatihan a 
	INNER JOIN kategori_pelatihan b
		ON a.kategori_pelatihan_id = b.kategori_pelatihan_id
		ORDER BY materi_pelatihan_id ASC";
	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
						'href'=>'index.php?com=materi_pelatihan&task=editmateri_pelatihan&id='.$ey['materi_pelatihan_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$ey['materi_pelatihan_id'].');',
						'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'),  
			'l'=>  $ey['materi_pelatihan_label']  ,    
			'd'=>  $ey['kategori_pelatihan_label']  ,    
			'operasi'=> position_text_align(  $edit_button.' '.$delete_button , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row);  
	//'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com=report&task=all_materi_pelatihan\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');')
	return table_builder($header , $datas ,  5 , false );
}

function form_materi_pelatihan_validate($id){
	$errsubmit = false;
	$err = array();
	 
	$code =  trim($_POST["label"]); 
	if( $code == ''){
		$errsubmit = true;
		$err[] = "Materi Pelatihan belum diisi";
	}
	
	$query="SELECT materi_pelatihan_id FROM materi_pelatihan 
		WHERE materi_pelatihan_label='".$code.
		"' AND materi_pelatihan_id<>".$id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Materi Pelatihan sudah digunakan";	
	}
  
	$code =  trim($_POST["kategori"]); 
	if( $code == '0'){
		$errsubmit = true;
		$err[] = "Kategori belum diisi";
	}
	
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function form_materi_pelatihan_submit($id){
	$datas = array( 
		'kategori_pelatihan_id'=>my_type_data_int( $_POST['kategori'] ),  
		'materi_pelatihan_label'=>my_type_data_str( $_POST['label'] ),  
		'datetime_added'=>my_type_data_function( 'NOW()' ),  
	);
	if($id > 0){
		return my_update_record('materi_pelatihan' ,'materi_pelatihan_id' , $id , $datas);
	}
	
	return my_insert_record('materi_pelatihan' , $datas);
}

function form_materi_pelatihan_create($id = 0){
	$view = form_header( "materi_pelatihan" , "materi_pelatihan"  );
 	$fields = my_get_data_by_id('materi_pelatihan','materi_pelatihan_id',$id);
	 
 
	$label = array(
				'name'=>'label',
				'value'=>( isset($_POST['label']) ? $_POST['label'] : $fields['materi_pelatihan_label'] ),
				'id'=>'label',
				'type'=>'label',
				'size'=>'45'
			);
	$form_field_major = form_dynamic($label);
	
	$view .= form_field_display( $form_field_major  , "Materi pelatihan"  );   
	
	
	//KATEGORI
	$query = "SELECT * FROM kategori_pelatihan";
	$result = my_query($query);
	$kategoris = array();
	while($rkategori = my_fetch_array($result)){
		$kategoris[$rkategori['kategori_pelatihan_id']] = $rkategori['kategori_pelatihan_label'];
	}
	$kategori = array(
				'name'=>'kategori',
				'value'=>( isset($_POST['kategori']) ? $_POST['kategori'] : $fields['kategori_pelatihan_id'] ),
				'id'=>'kategori', 
			);
	$form_field_kategori = form_dropdown($kategori, $kategoris );
	
	$view .= form_field_display( $form_field_kategori  , "Kategori pelatihan"  );   
	
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
} 
 
 
function delete_materi($id){
	$query = "DELETE FROM materi_pelatihan WHERE materi_pelatihan_id ={$id} ";
	return my_query($query);
}	
